﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using Ccit.ClassLibrary;
using System.Collections.Generic;
using Ccit.SysPlat.Organ;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.UserControls
{
    public partial class sys_range_select : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidOrganId.Value = (null != Request.QueryString["organId"]) ? Request.QueryString["organId"].ToString() : string.Empty;
                _hidRangeType.Value = (null != Request.QueryString["rangetype"]) ? Request.QueryString["rangetype"].ToString() : "0";
                _hidSeparator.Value = (null != Request.QueryString["sep"]) ? Request.QueryString["sep"].ToString() : string.Empty;
            }
        }

        private List<List<MsKeyValue>> getDataByType(string rangeType, string selIds, string organId,MsRangeClause msRangeClause)
        {   
            eSysRangeType enRangeType = (eSysRangeType)Enum.Parse(typeof(eSysRangeType), rangeType);
            List<MsKeyValue> lstUnSel = null;  //未选中项集合
            List<MsKeyValue> lstSel = null;    //选中项集合
            string findUnSel = (null != msRangeClause && !string.IsNullOrEmpty(msRangeClause.findUnSel)) ? msRangeClause.findUnSel : string.Empty;
            string findSel = (null != msRangeClause && !string.IsNullOrEmpty(msRangeClause.findSel)) ? msRangeClause.findSel : string.Empty;
            switch (enRangeType) {
                case eSysRangeType.USER:
                    //IUser _userProcessor = Factory.CreateObject(MsUser.assemblyName,MsUser.className,ImportComponentType.DotNetDll,null) as IUser;
                    //lstUnSel = _userProcessor.GetUserListByOrgan(organId, selIds, eTrueFalse.FALSE);
                    //lstSel = (!string.IsNullOrEmpty(selIds)) ? _userProcessor.GetUserListByIds(selIds) : new List<MsKeyValue>();
                    //_userProcessor = null;
                    return getUserDataByClause(rangeType, selIds, organId, msRangeClause);
                    break;
                case eSysRangeType.DEPT:
                    IDept _deptProcessor = Factory.CreateObject(MsDept.assemblyName, MsDept.className, ImportComponentType.DotNetDll, null) as IDept;                    
                    lstUnSel = _deptProcessor.GetDeptListByOrgan(organId, selIds, findUnSel);                    
                    lstSel = (!string.IsNullOrEmpty(selIds)) ? _deptProcessor.GetDeptListByIds(selIds,findSel) : new List<MsKeyValue>();
                    _deptProcessor = null;
                    break;
                case eSysRangeType.POS:
                    IPos _posProcessor = Factory.CreateObject(MsPos.assemblyName, MsPos.className, ImportComponentType.DotNetDll, null) as IPos;
                    lstUnSel = _posProcessor.GetPosListByOrgan(organId, selIds,findUnSel);
                    lstSel = (!string.IsNullOrEmpty(selIds)) ? _posProcessor.GetPosListByIds(selIds,findSel) : new List<MsKeyValue>();
                    _posProcessor = null;
                    break;
                case eSysRangeType.CLS:
                    IClass _clsProcessor = Factory.CreateObject(MsClass.assemblyName, MsClass.className, ImportComponentType.DotNetDll, null) as IClass;
                    lstUnSel = _clsProcessor.GetClsListByOrgan(organId, selIds,findUnSel);
                    lstSel = (!string.IsNullOrEmpty(selIds)) ? _clsProcessor.GetClsListByIds(selIds,findSel) : new List<MsKeyValue>();
                    _clsProcessor = null;
                    break;
            }

            return new List<List<MsKeyValue>> { lstUnSel, lstSel };      
        }
        

        protected void lnbCallBack_Click(object sender, EventArgs e)
        {
            _hidLoadSign.Value = "1";
            string rangeType = _hidRangeType.Value;
            string selIds = _hidSelIds.Value;
            string organId = _hidOrganId.Value;
            string clauseExpr = _hidRangeClause.Value;  //条件
            MsRangeClause msRangeClause = (string.IsNullOrEmpty(clauseExpr)) ? null :
                            JavaScriptConvert.DeserializeObject(clauseExpr, typeof(MsRangeClause)) as MsRangeClause;                

            List<List<MsKeyValue>> lstData = getDataByType(rangeType, selIds, organId,msRangeClause);
            CtrInterface.BindListBox(ref lstbOrigin, lstData[0], false, string.Empty);
            CtrInterface.BindListBox(ref lstbDest, lstData[1], false, string.Empty);

            lstData = null;
            msRangeClause = null;
        }

        //protected void lnbClauseCall_Click(object sender, EventArgs e)
        //{
        //    string rangeType = _hidRangeType.Value;
        //    string selIds = _hidRangeId.Value;
        //    string organId = _hidOrganId.Value;
        //    string clauseExpr = _hidRangeClause.Value;
        //    MsRangeClause msRangeClause = JavaScriptConvert.DeserializeObject(clauseExpr, typeof(MsRangeClause)) as MsRangeClause;

        //    List<List<MsKeyValue>> lstData = getUserDataByClause(rangeType, selIds, organId, msRangeClause);
        //    CtrInterface.BindListBox(ref lstbOrigin, lstData[0], false, string.Empty);
        //    CtrInterface.BindListBox(ref lstbDest, lstData[1], false, string.Empty);

        //    msRangeClause = null;
        //    lstData = null;            
        //}

        private List<List<MsKeyValue>> getUserDataByClause(string rangeType, string selIds, string organId, MsRangeClause msRangeClause)
        {
            string whereClause = " 1=1 ";
            if (!string.IsNullOrEmpty(selIds))
                whereClause += " AND A.SYS_A1_10 NOT IN (" + selIds + ")";

            string findSel = string.Empty;
            if (null != msRangeClause)
            {
                setDeptClause(msRangeClause.deptClause, ref whereClause);
                setPosClause(msRangeClause.posClause, ref whereClause);
                setClsClause(msRangeClause.clsClause, ref whereClause);

                string findUnSel = (!string.IsNullOrEmpty(msRangeClause.findUnSel)) ? msRangeClause.findUnSel.Trim() : string.Empty;                
                whereClause += (!string.IsNullOrEmpty(findUnSel)) ? " AND A.SYS_A1_40 LIKE '%" + findUnSel + "%'" : string.Empty;
                findSel = (!string.IsNullOrEmpty(msRangeClause.findSel)) ? msRangeClause.findSel.Trim() : string.Empty;    
            }

            IUser _userProcessor = Factory.CreateObject(MsUser.assemblyName, MsUser.className, ImportComponentType.DotNetDll, null) as IUser;
            List<MsKeyValue> lstUnSel = _userProcessor.GetUserListByOrgan(organId, eTrueFalse.FALSE, whereClause);
            List<MsKeyValue> lstSel = (!string.IsNullOrEmpty(selIds)) ? _userProcessor.GetUserListByIds(selIds,findSel) : new List<MsKeyValue>();
            _userProcessor = null;

            return new List<List<MsKeyValue>> { lstUnSel, lstSel };            
        }

        private void setDeptClause(MsDeptClause deptClause, ref string whereClause)
        {
            if (null == deptClause) return;

            if (deptClause.isALL == eTrueFalse.TRUE) return;

            if(deptClause.nonDept == eTrueFalse.TRUE)
                whereClause += " AND A.SYS_A1_10 NOT IN (SELECT SYS_R1_10 FROM SYS_R1)";

            string deptIds = string.Empty;             
            if (deptClause.isContainSub == eTrueFalse.TRUE)
            {
                if (!string.IsNullOrEmpty(deptClause.selDeptIds))
                {
                    IDept _deptProcessor = Factory.CreateObject(MsDept.assemblyName, MsDept.className, ImportComponentType.DotNetDll, null) as IDept;
                    deptIds = _deptProcessor.findALLDeptIds(deptClause.selDeptIds);
                    whereClause += " AND B.SYS_A2_10 IN (" + deptIds + ")";
                    _deptProcessor = null;
                }
            }
            else
            {
                if(!string.IsNullOrEmpty(deptClause.selDeptIds))
                    whereClause += " AND B.SYS_A2_10 IN (" + deptClause.selDeptIds + ")";
            }
        }

        private void setPosClause(MsPosClause posClause, ref string whereClause)
        {
            if (null == posClause) return;

            if (posClause.isALL == eTrueFalse.TRUE) return;

            if(posClause.nonPos == eTrueFalse.TRUE)
                whereClause += " AND A.SYS_A1_10 NOT IN (SELECT SYS_R2_10 FROM SYS_R2)";

            if(!string.IsNullOrEmpty(posClause.selPosIds))
                whereClause += " AND C.SYS_A3_10 IN (" + posClause.selPosIds + ")";  
        }

        private void setClsClause(MsClsClause clsClause, ref string whereClause)
        {
            if (null == clsClause) return;

            if (clsClause.isALL == eTrueFalse.TRUE) return;

            if(clsClause.nonCls == eTrueFalse.TRUE)
                whereClause += " AND A.SYS_A1_10 NOT IN (SELECT SYS_R8_10 FROM SYS_R8)";

            if(!string.IsNullOrEmpty(clsClause.selClsIds))
                whereClause += " AND D.SYS_A5_10 IN (" + clsClause.selClsIds + ")";           
        }
    }
}
